package yur.wedding.modules.sys.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import yur.wedding.common.utils.Query;

import yur.wedding.modules.sys.dao.SysDocumentDocsubcategoryDao;
import yur.wedding.modules.sys.entity.SysDocumentDocsubcategoryEntity;
import yur.wedding.modules.sys.service.SysDocumentDocsubcategoryService;
import yur.wedding.utils.PageUtils;


@Service("sysDocumentDocsubcategoryService")
public class SysDocumentDocsubcategoryServiceImpl extends ServiceImpl<SysDocumentDocsubcategoryDao, SysDocumentDocsubcategoryEntity> implements SysDocumentDocsubcategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysDocumentDocsubcategoryEntity> page = this.page(
                new Query<SysDocumentDocsubcategoryEntity>().getPage(params),
                new QueryWrapper<SysDocumentDocsubcategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 只查询不包括按钮  用于弹框 添加目录或者修改等
     *
     * @return
     */
    @Override
    public List<SysDocumentDocsubcategoryEntity> queryNotButtonList(String parentName) {


        return baseMapper.queryNotButtonList(parentName);
    }


    /**
     * 模糊查找目录   递归查询父类
     *
     * @param parentName
     * @return
     */
    @Override
    public List<SysDocumentDocsubcategoryEntity> queryNotButtonLikeList(String parentName) {
        List<SysDocumentDocsubcategoryEntity> list = baseMapper.queryNotButtonList(parentName);
        if (list.size()>0){
            Long [] snm=new Long[list.size()];
            for (int i = 0; i < list.size(); i++) {
                snm[i]=list.get(i).getMaincategoryId();
            }
            for (int s = 0; s < snm.length; s++) {
                getParentTreeList(snm[s], list);
            }

        }


        return list;
    }


    private List<SysDocumentDocsubcategoryEntity> getParentTreeList(Long maincategoryId, List<SysDocumentDocsubcategoryEntity> list) {


        SysDocumentDocsubcategoryEntity parentTree = getParentTree(maincategoryId);
        if (parentTree!=null){
            list.add(parentTree);
            if (parentTree.getMaincategoryId()!=0){
                getParentTreeList(parentTree.getMaincategoryId(),list);
            }
        }


        return list;
    }


    /**
     * 通过子类 查父类
     * @param maincategoryId
     * @return
     */
    private SysDocumentDocsubcategoryEntity getParentTree(Long maincategoryId) {

        return baseMapper.selectById(maincategoryId);
    }


    /**
     * 通过父类目录查是否存在子目录  或子文件
     *
     * @param dbgyIds
     * @return
     */
    @Override
    public List<SysDocumentDocsubcategoryEntity> qeryListParentId(Long dbgyIds) {


        return baseMapper.qeryListParentId(dbgyIds);
    }

}
